const mongoose = require("./mgdb.js");
const { DataRls } = require("../config/class.js");

const groupChatSchema = new mongoose.Schema({
	groupName: {
		type: String,
		required: true
	},
	power: [{
		powerId: {
			type: mongoose.Schema.Types.ObjectId
		},
		level: {
			type: Number,
			enum: [0, 1, 2]
		}
	}],
	members: [{
		memberId: {
			type: mongoose.Schema.Types.ObjectId
		},
		username: {
			type: String
		}
	}],
	createTime: {
		type: Date,
		default: Date.now
	},
	groupType: {
		type: String,
		enum: ['public', 'private'],
		default: 'public'
	},
	announcement: {
		type: String
	},
	groupAvatar: {
		type: String
		// 可以是头像的 URL 或其他标识
	},
	// 新增字段：群聊描述
	description: {
		type: String
	}
});

// 创建模型
const GroupChat = mongoose.model('GroupChat', groupChatSchema);

// 创建群聊的异步函数
async function createGroupChat(groupData) {
	try {
		const newGroup = new GroupChat(groupData);
		const savedGroup = await newGroup.save();
		return new DataRls(`创建群聊成功`, savedGroup, true);
	} catch (error) {
		return new DataRls(`创建群聊失败`, null, false, error);
	}
}

// 添加新用户的异步函数
async function addMemberToGroupChat(groupId, memberData) {
	try {
		// 查找群聊
		const group = await GroupChat.findById(groupId);
		if (!group) {
			return new DataRls(`没能根据 id 找到群聊: ${groupId}`, null, false);
		}
		// 将成员数据添加到 members 数组
		group.members.push(memberData);
		// 保存更新后的群聊
		const updatedGroup = await group.save();
		return new DataRls(`增加群聊成员成功: ${groupId}`, updatedGroup, true);
	} catch (error) {
		return new DataRls(`增加群聊成员失败: ${groupId}`, null, false, error);
	}
}

// 根据 id 查询群聊的异步函数
async function findGroupChatById(groupId) {
	try {
		const groupData = await GroupChat.findById(groupId);
		if (!groupData) {
			return new DataRls(`没能根据 id 找到群聊: ${groupId}`, null, false);
		}
		return new DataRls(`群聊对象已得到: ${groupId}`, groupData, true);
	} catch (error) {
		return new DataRls(`群聊对象获取失败: ${groupId}`, null, false, error);
	}
}

// 获取所有群聊列表的异步函数
async function getGroupChats(query = '') {
	try {
		let groupChats;
		if (!query) {
			groupChats = await GroupChat.find();
		} else {
			groupChats = await GroupChat.find({ groupName: { $regex: query, $options: 'i' } });
		}
		return new DataRls(`已获取群聊${query ? ` 查询 '${query}'` : ''}`, groupChats, groupChats.length > 0);
	} catch (error) {
		return new DataRls(`获取群聊时出错${query ? ` 查询 '${query}'` : ''}`, null, false, error);
	}
}

// 更新群聊信息的异步函数
async function updateGroupChat(groupId, updatedData) {
	try {
		const updatedGroup = await GroupChat.findByIdAndUpdate(groupId, updatedData, { new: true });
		if (!updatedGroup) {
			return new DataRls(`没能根据 id 更新群聊: ${groupId}`, null, false);
		}
		return new DataRls(`群聊更新成功: ${groupId}`, updatedGroup, true);
	} catch (error) {
		return new DataRls(`群聊更新失败: ${groupId}`, null, false, error);
	}
}

// 删除群聊的异步函数
async function deleteGroupChat(groupId) {
	try {
		const deletedGroup = await GroupChat.findByIdAndDelete(groupId);
		if (!deletedGroup) {
			return new DataRls(`没能根据 id 删除群聊: ${groupId}`, null, false);
		}
		return new DataRls(`群聊删除成功: ${groupId}`, deletedGroup, true);
	} catch (error) {
		return new DataRls(`群聊删除失败: ${groupId}`, null, false, error);
	}
}

// 获取群聊成员列表的异步函数
async function getGroupMembers(groupId) {
	try {
		const group = await GroupChat.findById(groupId);
		if (!group) {
			return new DataRls(`没能根据 id 找到群聊: ${groupId}`, null, false);
		}
		return new DataRls(`群聊成员已获取: ${groupId}`, group.members, true);
	} catch (error) {
		return new DataRls(`获取群聊成员失败: ${groupId}`, null, false, error);
	}
}

module.exports = {
	GroupChat,
	getGroupChats,
	createGroupChat,
	findGroupChatById,
	addMemberToGroupChat,
	updateGroupChat,
	deleteGroupChat,
	getGroupMembers
};